package cn.web.tunnelrobot.service.impl;

import cn.web.tunnelrobot.dao.ImageInfoDao;
import cn.web.tunnelrobot.dao.UserRecordDao;
import cn.web.tunnelrobot.dto.ImageInfoDTO;
import cn.web.tunnelrobot.pojo.User;
import cn.web.tunnelrobot.pojo.UserRecord;
import cn.web.tunnelrobot.pojo.WorkArea;
import cn.web.tunnelrobot.service.ImageInfoService;
import cn.web.tunnelrobot.util.Constants;
import cn.web.tunnelrobot.util.MileageConversion;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 影像采集信息service实现层
 * @Author:lichuang
 */
@Service
public class ImageInfoServiceImpl implements ImageInfoService {
    @Autowired
    private ImageInfoDao imageInfoDao;
    @Autowired
    private UserRecordDao userRecordDao;
    //获取用户session信息
    //HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    //User user = (User) request.getSession().getAttribute(Constants.USER_SESSION);
    @Override
    public HashMap<String, Object> getImageInfoByPrIdAndMileage(Integer prId, Integer imMileage) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        /*UserRecord userRecord = new UserRecord();
        userRecord.setUsId(user.getUsId());
        userRecord.setUrOperatContent("进入实施控制-采集信息页面");
        userRecord.setUrOperatTime(new Date());
        userRecordDao.addUserRecord(userRecord);*/
        try {
            List<ImageInfoDTO> imageInfos = imageInfoDao.getImageInfoByPrIdAndMileage(prId,imMileage);
            List<ImageInfoDTO> imageInfos1 = new ArrayList<>();
            for (ImageInfoDTO infoDTO:imageInfos ) {
                ImageInfoDTO imageInfo = new ImageInfoDTO();
                imageInfo = infoDTO;
                //施工状态(上 1开挖、2初支、3二衬 、4已完成)(下  1仰拱、2已完成)
                if(infoDTO.getImBelongsPlate() == 1){
                    if (infoDTO.getImConstructionStatus() == 1){
                        imageInfo.setImConstructionStatusName("开挖");
                    }else if (infoDTO.getImConstructionStatus() == 2){
                        imageInfo.setImConstructionStatusName("初支");
                    }else if (infoDTO.getImConstructionStatus() == 3){
                        imageInfo.setImConstructionStatusName("仰拱");
                    }else if (infoDTO.getImConstructionStatus() == 4){
                        imageInfo.setImConstructionStatusName("已完成");
                    }
                }else if(infoDTO.getImBelongsPlate() == 2){
                    if (infoDTO.getImConstructionStatus() == 1){
                        imageInfo.setImConstructionStatusName("仰拱");
                    }else if (infoDTO.getImConstructionStatus() == 2){
                        imageInfo.setImConstructionStatusName("已完成");
                    }
                }
                imageInfos1.add(imageInfo);
            }
            if(imageInfos.size() > 0 ){
                map.put("data",imageInfos1);
                map.put("code",0);
                return map;
            }else{
                map.put("data","查询暂无信息");
                map.put("code",-1);
                return map;
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("data","查询信息时出现问题");
            map.put("code",-1);
            return map;
        }
    }

    @Override
    public HashMap<String, Object> addImageInfo(ImageInfoDTO imageInfoDTO) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        if(Constants.is_intranet == 1){
            map.put("code",-1);
            map.put("msg","外网没有权限");
            return map;
        }
        if(StringUtils.isNotEmpty(imageInfoDTO.getImMileage())){
            //判断里程是不是数字型
            boolean flag =  MileageConversion.isNumeric(imageInfoDTO.getImMileage());
            if(flag == false){
                int dist = MileageConversion.distance(imageInfoDTO.getImMileage());
                if(dist%10!=0){
                    map.put("data","里程数值错误");
                    map.put("code",-1);
                    return map;
                }
            }
        }else{
            map.put("data","请填写里程");
            map.put("code",-1);
            return map;
        }
        if(StringUtils.isEmpty(imageInfoDTO.getImVideoAddress())){
            map.put("data","文件地址为空");
            map.put("code",-1);
            return map;
        }
        //获取用户所绑定的项目ID
        //WorkArea workArea1 = workAreaDao.getWorkAreaByUsId(imageInfoDTO.getImCreateBy());
        //imageInfoDTO.setPrId(workArea1.getPrId());
        StringBuilder sbu = new StringBuilder();
        sbu.append("判定隧道距离:"+imageInfoDTO.getImMileage()+"m处,判定为:");
        if(imageInfoDTO.getImDecideStatus() == 1){//1合格 2不合格  3忽略
            sbu.append("合格");
        }else  if(imageInfoDTO.getImDecideStatus() == 2){
            sbu.append("不合格");
        }else  if(imageInfoDTO.getImDecideStatus() == 3){
            sbu.append("忽略");
        }
        try {
            int result = imageInfoDao.addImageInfo(imageInfoDTO);
            if(result > 0){
                map.put("data","判定成功");
                map.put("code",0);
                /*UserRecord userRecord = new UserRecord();
                userRecord.setUsId(user.getUsId());
                userRecord.setUrOperatContent(sbu.toString());
                userRecord.setUrOperatTime(new Date());
                userRecordDao.addUserRecord(userRecord);*/
                return map;
            }else{
                map.put("data","判定失败");
                map.put("code",-1);
                return map;
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("data","判定信息时出错");
            map.put("code",-1);
            return map;
        }
    }
    @Override
    public HashMap<String, Object> getImageInfoList(Integer prId, Integer imMileage, Integer isId, Integer imBelongsPlate, Integer imConstructionStatus, Integer imDecideStatus, Integer pageNo, Integer pageSize) {
        HashMap<String,Object> map = new HashMap<>();
        try {
            int count = imageInfoDao.getImageInfoListCount(prId,imMileage,isId,imBelongsPlate,imConstructionStatus,imDecideStatus);
            if (count > 0){
                List<ImageInfoDTO> imageInfoList = imageInfoDao.getImageInfoList(prId,imMileage,isId,imBelongsPlate,imConstructionStatus,imDecideStatus,(pageNo-1)*pageSize,pageSize);
                for (ImageInfoDTO infoDTO:imageInfoList) {
                    if(infoDTO.getImBelongsPlate() == 1){
                        if (infoDTO.getImConstructionStatus() == 1){
                            infoDTO.setImConstructionStatusName("开挖");
                        }else if (infoDTO.getImConstructionStatus() == 2){
                            infoDTO.setImConstructionStatusName("初支");
                        }else if (infoDTO.getImConstructionStatus() == 3){
                            infoDTO.setImConstructionStatusName("仰拱");
                        }else if (infoDTO.getImConstructionStatus() == 4){
                            infoDTO.setImConstructionStatusName("已完成");
                        }
                    }else if(infoDTO.getImBelongsPlate() == 2){
                        if (infoDTO.getImConstructionStatus() == 1){
                            infoDTO.setImConstructionStatusName("仰拱");
                        }else if (infoDTO.getImConstructionStatus() == 2){
                            infoDTO.setImConstructionStatusName("已完成");
                        }
                    }
                }
                map.put("code",0);
                map.put("data",imageInfoList);
                map.put("count",count);
            }else {
                map.put("code",-1);
                map.put("count",0);
                map.put("msg","暂无列表信息");
            }
        } catch (Exception e) {
            map.put("code",-1);
            map.put("msg","查询首页隧道图片/影视信息列表时出错");
            e.printStackTrace();
        }
        return map;
    }
    /**
     * getImageInfoListNew
     * gbq
     */
    @Override
    public HashMap<String, Object> getImageInfoListNew(ImageInfoDTO imageInfoDTO) {
        HashMap<String,Object> map = new HashMap<>();
        try {
            List<ImageInfoDTO> imageInfoList = imageInfoDao.getImageInfoListNew(imageInfoDTO);
            if(imageInfoList.size()>0){
                for(int i=0;i<imageInfoList.size();i++){
                    String DK=MileageConversion.mileage(Float.parseFloat(imageInfoList.get(i).getImMileage()));
                    imageInfoList.get(i).setMileageDK(DK);
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String dateString = formatter.format(imageInfoList.get(i).getImCreateDate());
                    imageInfoList.get(i).setImCreateDateFormat(dateString);
                    String url=imageInfoList.get(i).getImVideoAddress();
                    String suffix=url.substring(url.indexOf('.')+1);
                    if(suffix.equals("jpg")||suffix.equals("png")||suffix.equals("jpeg")){
                        imageInfoList.get(i).setType(0);
                    }else if(suffix.equals("mp4")){
                        imageInfoList.get(i).setType(1);
                    }
                    imageInfoList.get(i).setImVideoAddress("https://tkws.tgmis.cn:10011"+url);
                }
                map.put("code",0);
                map.put("data",imageInfoList);
            }else {
                map.put("code",-1);
                map.put("count",0);
                map.put("msg","暂无列表信息");
            }

        } catch (Exception e) {
            map.put("code",-1);
            map.put("msg","查询首页隧道图片/影视信息列表时出错");
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public HashMap<String, Object> getImageInfoListFY(ImageInfoDTO imageInfoDTO, Integer pageNo, Integer pageSize) {
        HashMap<String,Object> map = new HashMap<>();
        try {
            List<ImageInfoDTO> imageInfoList = imageInfoDao.getImageInfoListFY(imageInfoDTO,(pageNo-1)*pageSize,pageSize);
            if(imageInfoList.size()>0){
                for(int i=0;i<imageInfoList.size();i++){
                    String DK=MileageConversion.mileage(Float.parseFloat(imageInfoList.get(i).getImMileage()));
                    imageInfoList.get(i).setMileageDK(DK);
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String dateString = formatter.format(imageInfoList.get(i).getImCreateDate());
                    imageInfoList.get(i).setImCreateDateFormat(dateString);
                    String url=imageInfoList.get(i).getImVideoAddress();
                    String suffix=url.substring(url.indexOf('.')+1);
                    if(suffix.equals("jpg")||suffix.equals("png")||suffix.equals("jpeg")){
                        imageInfoList.get(i).setType(0);
                    }else if(suffix.equals("mp4")){
                        imageInfoList.get(i).setType(1);
                    }
                    imageInfoList.get(i).setImVideoAddress("https://tkws.tgmis.cn:10011"+url);
                }
                map.put("code",0);
                int count=imageInfoDao.getImageInfoListFYCount(imageInfoDTO);
                map.put("count",count);
                map.put("data",imageInfoList);
            }else {
                map.put("code",-1);
                map.put("count",0);
                map.put("msg","暂无列表信息");
            }

        } catch (Exception e) {
            map.put("code",-1);
            map.put("msg","查询首页隧道图片/影视信息列表时出错");
            e.printStackTrace();
        }
        return map;
    }

}
